package edu.dayu.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import edu.dayu.dao.EsObjectDao;
import edu.dayu.mapper.ActivityMapper;
import edu.dayu.pojo.Activity;
import edu.dayu.service.ActivityService;
import edu.dayu.vo.EsObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ActivityServiceImpl implements ActivityService {
    @Autowired
    private ActivityMapper activityMapper;

    @Autowired
    private EsObjectDao esObjectDao;

    @Override
    public PageInfo<Activity> activityList(Activity activity) {
        PageHelper.startPage(activity.getPageNum(),activity.getPageSize());
        List<Activity> activitys = activityMapper.activityList(activity);
        PageInfo<Activity> articlePageInfo = new PageInfo<>(activitys);
        return articlePageInfo;
    }

    @Override
    public void activityInsert(Activity activity) {
        activityMapper.activityInsert(activity);
//        esObjectDao.save(beanUtil(activity));
    }

    @Override
    public void activityUpdate(Activity activity) {
        activityMapper.activityUpdate(activity);
        if (activity.getStatus()==2)
        esObjectDao.save(beanUtil(activity));
    }

    @Override
    public void activityDelete(Activity activity) {
        activityMapper.activityDelete(activity);
        esObjectDao.delete(beanUtil(activity));
    }

    /**
     * 转换
     * @param activity
     * @return
     */
    public EsObject beanUtil(Activity activity){
        EsObject esObject = new EsObject();
        BeanUtils.copyProperties(activity,esObject);
        esObject.setLink("社团活动");
        esObject.setObjectId(activity.getId());
        esObject.setId("0"+activity.getId().toString());
        return esObject;
    }

}
